package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.api.feign.paas.MemberConfigControllerFeign;
import com.ssy.lingxi.system.dao.FieldsVerifyRepository;
import com.ssy.lingxi.system.dto.request.FieldsVerifyAddRequest;
import com.ssy.lingxi.system.dto.request.FieldsVerifyDeleteRequest;
import com.ssy.lingxi.system.dto.request.FieldsVerifyListRequest;
import com.ssy.lingxi.system.dto.response.MiddlegroundUserLoginResponse;
import com.ssy.lingxi.system.entity.FieldsVerify;
import com.ssy.lingxi.system.model.response.MemberConfigCheckRuleVO;
import com.ssy.lingxi.system.service.FieldsVerifyService;
import com.ssy.lingxi.system.utils.UserDetailsUtil;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class FieldsVerifyServiceImpl implements FieldsVerifyService {
    @Resource
    FieldsVerifyRepository fieldsVerifyRepository;
    @Resource
    UserDetailsUtil userDetailsUtil;
    @Resource
    MemberConfigControllerFeign memberConfigControllerFeign;
    @Transactional
    @Override
    public Wrapper addFieldsVerify(HttpServletRequest httpServletRequest,FieldsVerifyAddRequest request) {
        MiddlegroundUserLoginResponse loginUserDetails = userDetailsUtil.getLoginUserDetails(httpServletRequest);
        fieldsVerifyRepository.deleteByMemberConfigIdEquals(request.getFieldsVerifys().get(0).getMemberConfigId());
        fieldsVerifyRepository.saveAll( request.getFieldsVerifys());
        return Wrapper.success();
    }


    @Transactional
    @Override
    public Wrapper configMemberFieldsVerifyDelete(FieldsVerifyDeleteRequest request) {
        fieldsVerifyRepository.deleteById(request.getId());
        MemberConfigCheckRuleVO memberConfigCheckRuleVO=new MemberConfigCheckRuleVO();
        memberConfigCheckRuleVO.setId(request.getId());
        return Wrapper.success();
    }

    @Override
    public Wrapper<List<FieldsVerify>> configMemberFieldsVerifyList(FieldsVerifyListRequest request) {
        FieldsVerify fieldsVerify = new FieldsVerify();
        fieldsVerify.setMemberConfigId(request.getId());
        Example<FieldsVerify> example = Example.of(fieldsVerify);
        List<FieldsVerify> all = fieldsVerifyRepository.findAll(example);
        return Wrapper.success(all);
    }
}
